package com.mos.action;

import com.mos.base.sql.Page;
import com.mos.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016-03-27.
 */
@Controller
@RequestMapping("/demo")
public class DemoAction {

    private static final String prefix = "/demo/";

    @Autowired
    private UserService userService;

    @RequestMapping("/{id}")
    public String demo(@PathVariable("id")String id, Model model){
        System.out.println(id);
        model.addAttribute("id",id);
        return prefix + "index";
    }

    @RequestMapping("/upload")
    public String upload(){
        return prefix + "upload";
    }

    @RequestMapping("/demo/{auto}")
    public String auto(@PathVariable("auto")String auto){
        return prefix + auto;
    }

    @ResponseBody
    @RequestMapping("/demo/json")
    public Page json(@RequestParam("pageSize") Integer pageSize,@RequestParam("pageNumber") Integer pageNumber){
        if(pageNumber != 0){
            pageNumber = (pageNumber/pageSize) ;
        }
        pageNumber = pageNumber + 1;
        Page page = userService.queryPage(pageNumber,pageSize,null);
       /* Page page = new Page();
        List list = new ArrayList();
        for (int i=0;i<1500;i++){
            Map map = new HashMap();
            map.put("id","id_"+i);
            map.put("name","name_"+i);
            map.put("age",i+9);
            list.add(map);
        }
        page.setTotal(list.size());
        page.setRows(list);
        page.setCurrentPage(pageNumber);
        page.setNumPerPage(pageSize);*/
        return page;
    }
}
